<script>

	function deptChange(){
		var filter = {"is_active":1,"department_id":$("#department_id").val()};
		var data = {obj:"MTahunAjaran",filter: filter,value_field:"id",title_field:"name"};
		renewSelectData(data, "#tahun_ajaran_id");
		$("#kelas_tahun_ajaran_id_asal").val("");
		$("#kelas_tahun_ajaran_name_asal").val("");
		$("#tingkatan_id").val("");
		$('#tblData').jqGrid('clearGridData');
		$("#kelas_tahun_ajaran_id_tujuan").val("");
		$("#kelas_tahun_ajaran_name_tujuan").val("");
		$('#tblData2').jqGrid('clearGridData');
	}
	
	
	function refreshGrid(){
		$("#tblData").jqGrid("setGridParam",{
			postData:{
				"kelas_tahun_ajaran_id" :$("#kelas_tahun_ajaran_id_asal").val(),
			},
			datatype:"json"
		}).trigger("reloadGrid");
	}
	
	function refreshGrid2(){
		$("#tblData2").jqGrid("setGridParam",{
			postData:{
				"kelas_tahun_ajaran_id" :$("#kelas_tahun_ajaran_id_tujuan").val(),
			},
			datatype:"json"
		}).trigger("reloadGrid");
	}
	
	function kelasTahunAjaranAsalBeforePopup(){
		var element = "<input type='text' name='tahun_ajaran_id' value='" + $("#tahun_ajaran_id").val() + "'/>";
		//$('#frmFilter').attr("");
		$('#frmFilter').html(element);		
		//alert("Tahun Ajaran Before popup");
		
	}
	function kelasTahunAjaranTujuanBeforePopup(){
		var element = "<input type='text' name='tahun_ajaran_id' value='" + $("#tahun_ajaran_id").val() + "'/>";
		element += "<input type='text' name='tingkatan_id' value='"+$("#tingkatan_id").val()+"'/>";
		$('#frmFilter').attr("action","index.php?r=");
		$('#frmFilter').html(element);		
		//alert("Tahun Ajaran Before popup");
		
	}
	function tahunAjaranOnChange(){
		$("#tingkatan_id").val("");
		$("#kelas_tahun_ajaran_id_asal").val("");
		$("#kelas_tahun_ajaran_name_asal").val("");
		$('#tblData').jqGrid('clearGridData');
		$("#kelas_tahun_ajaran_id_tujuan").val("");
		$("#kelas_tahun_ajaran_name_tujuan").val("");
		$('#tblData2').jqGrid('clearGridData');		
	}
	
	
	function getKelasTahunAjaranAsal(obj){
		printConsole(obj);		
		oldValue = $("#kelas_tahun_ajaran_id_asal").val();
		if (obj ===""){
			$("#kelas_tahun_ajaran_id_asal").val("");
			$("#kelas_tahun_ajaran_name_asal").val("");
			$("#tingkatan_id").val("");
		}else{
			$("#kelas_tahun_ajaran_id_asal").val(obj.id);
			$("#kelas_tahun_ajaran_name_asal").val(obj.kelas_name);
			$("#tingkatan_id").val(obj.tingkatan_id);
		}
		if (oldValue != $("#kelas_tahun_ajaran_id_asal").val()){
			refreshGrid();
		}
	}
	
	function getKelasTahunAjaranTujuan(obj){
		oldValue = $("#kelas_tahun_ajaran_id_tujuan").val();
		if (obj ===""){
			$("#kelas_tahun_ajaran_id_tujuan").val("");
			$("#kelas_tahun_ajaran_name_tujuan").val("");
		}else{
			if (obj.id == $("#kelas_tahun_ajaran_id_asal").val()){
				alert("<?php echo Yii::t("dialog","Kelas tujuan tidak boleh sama dengan kelas asal !");?>");
				return;	
			}else{ 			
				$("#kelas_tahun_ajaran_id_tujuan").val(obj.id);
				$("#kelas_tahun_ajaran_name_tujuan").val(obj.kelas_name);
			}
		}
		
		if (oldValue != $("#kelas_tahun_ajaran_id_tujuan").val()){
			refreshGrid2();
		}
		
	}
	
	function moveToKelas(id){
		row = $("#tblData").jqGrid("getRowData",id);	
		printConsole(row);
		
		student_class_id = row.id
		
		if ($("#kelas_tahun_ajaran_id_tujuan").val() == ""){
			alert("<?php echo Yii::t("dialog","Kelas tujuan tidak boleh kosong !");?>");
			return;
		}
		if ($("#kelas_tahun_ajaran_id_asal").val() == $("#kelas_tahun_ajaran_id_tujuan").val()){
			alert("<?php echo Yii::t("dialog","Kelas tujuan tidak boleh sama dengan kelas asal !");?>");
			return;
		}

		if( ! confirm("<?php echo Yii::t("dialog","Pindahkan Kelas ?");?>") ){
			alert("batal");			
			return;
		}
		
		window.parent.showLoader();
		$.ajax({	
			url : "index.php?r=akademik/siswa/pindah_kelas/edit",
			type:"GET",
			async:true,
			data: {op: "move",student_class_id:student_class_id,kelas_tahun_ajaran_id_tujuan:$("#kelas_tahun_ajaran_id_tujuan").val()},
			success: function(data) {
				if(data.actionStatus == 0){
					//remove row from tblData
					$("#tblData").jqGrid("delRowData",id);
					$("#tblData").jqGrid().trigger("reloadGrid");
							
					//add row to tblData2
					row.id = data.id;
					$("#tblData2").jqGrid("addRowData",data.id,row,'first');
					$("#tblData2").jqGrid("sortGrid","id",true);
					
				}else if(data.actionStatus == 1){
					alert(data.actionMessage);
				}						
			},
			complete:function(){
				window.parent.hideLoader();
			}
		});
	}
	
		
	$(function(){
		$("#tblData").jqGrid({
			url:'index.php?r=akademik/siswa/penempatan_kelas/listKelas',
			postData:{"department_id":1,"activeOnly":1},
			datatype: 'local',
			loadonce:true,
			mtype:"GET",
			autowidth:true,
			height:"100%",
			altRows:true,
			ignoreCase:true,
			//$strFields="id,department_id,sid,name,if(gender='P','Pria','Wanita') as gender,birth_date,address,home_phone,handphone,email,ayah_nama,ibu_nama,wali_nama,is_active";
			colNames: ['id','SID','Nama','Jenis Kelamin','Action'],
			colModel: [				
				{name: 'id', index: 'id', width: 20,hidden:false,sorttype:'int',formatter:'int'},
				{name: 'sid', index: 'sid', width: 40,search:true},
				{name: 'student_name', index: 'student_name', width: 150,search:true},
				{name: 'gender', index: 'gender', width: 60,search:true},
				{name: 'act', index: 'act', width: 50, align: 'center'},
			],
			sortorder:"desc",
			gridview: true,
			rownumbers: false,
			rowNum: 10,
			rowList: [5, 10, 15],
			pager: '#navTblData',
			viewrecords: true,
			caption: 'Daftar Murid',
			jsonReader : { 
				root:"model",
				repeatitems: false			
			},
			loadComplete:function(data){
				console.log(data);
			},
			gridComplete: function(){ 
				var ids = jQuery("#tblData").getDataIDs(); 
				for(var i=0;i<ids.length;i++){ 
					var cl = ids[i]; 
					//btnEdit = "<input style='height:22px;width:20px;' type='button' value='E' onclick=jQuery('#rowed2').editRow("+cl+"); ></ids>"; 
					//btnDelete = "<input style='height:20px;width:20px;' type='button' value='S' onclick=jQuery('#rowed2').saveRow("+cl+"); />"; 
					btnEdit = "<img title='edit' class='imgBtn' style='height:20px;width:20px;' src='images/action/Actions-arrow-right-double-icon.png' onclick='moveToKelas(" + cl + ")' />"; 					
					jQuery("#tblData").setRowData(ids[i],{act:btnEdit}) 
				} 
			}
		});
		$("#tblData").jqGrid('navGrid','#navTblData',{add:false,edit:false,search:false,del:false,refresh:false});	
		

		//Grid ke Dua
		$("#tblData2").jqGrid({
			url:'index.php?r=akademik/siswa/penempatan_kelas/listKelas',
			postData:{"department_id":1,"activeOnly":1},
			datatype: 'local',
			loadonce:true,
			mtype:"GET",
			autowidth:true,
			height:"100%",
			altRows:true,
			ignoreCase:true,
			//$strFields="id,department_id,sid,name,if(gender='P','Pria','Wanita') as gender,birth_date,address,home_phone,handphone,email,ayah_nama,ibu_nama,wali_nama,is_active";
			colNames: ['id','SID','Nama','Jenis Kelamin'],
			colModel: [
				{name: 'id', index: 'id', width: 20,hidden:false,sorttype:'int',formatter:'int'},
				{name: 'sid', index: 'sid', width: 40,search:true},
				{name: 'student_name', index: 'student_name', width: 150,search:true},
				{name: 'gender', index: 'gender', width: 60,search:true},
			],
			sortorder:"desc",
			gridview: true,
			rownumbers: false,
			rowNum: 10,
			rowList: [5, 10, 15],
			pager: '#navTblData2',
			viewrecords: true,
			caption: 'Daftar Murid',
			jsonReader : { 
				root:"model",
				repeatitems: false			
			},
			loadComplete:function(data){
				console.log(data);
			},
		});
		$("#tblData2").jqGrid('navGrid','#navTblData2',{add:false,edit:false,search:false,del:false,refresh:false});		
		

		
	});
</script>

<form id="frmFilter" method="POST" style="display:none"></form>

<div style="width:1150px;margin-left:auto;margin-right:auto">
	<div>
		<label class="form" style="width:100px"><?php echo Yii::t("label","Department");?></label>
		<select id="department_id" name="department_id" onChange="deptChange()">
			<?php
				foreach($arrDepartment as $rw){
					$selected = $department_id == $rw->id ? "selected" :"";				
					echo "<option value='".$rw->id ."' $selected >". $rw->name ."</option>";
				}
			?>
		</select>
	</div>
	<div style="margin-bottom:10px">
		<label class="form" style="width:100px"><?php echo Yii::t("label","Tahun Ajaran");?></label>
		<select id="tahun_ajaran_id" name="tahun_ajaran_id" onChange="tahunAjaranOnChange()">
			<?php
				foreach($arrTahunAjaran as $rw){
					$selected = $tahun_ajaran_id == $rw->id ? "selected" :"";				
					echo "<option value='".$rw->id ."' $selected >". $rw->name ."</option>";
				}
			?>
		</select>
				
	</div>
	
	<hr>
	<style>
		#gview_tblData{
			min-height:250px;
		}
		#gview_tblData2{
			min-height:250px;
		}
		
	</style>
	<div style="float:left;width:500px">
		<div style="margin-bottom:10px">
			<label class="form mandatory" style="width:100px"><?php echo Yii::t("label","Kelas asal")?></label>
			<input type="hidden" id="kelas_tahun_ajaran_id_asal" name="kelas_tahun_ajaran_id_asal" value="" size="2" />		
			<input type="hidden" id="tingkatan_id" name="tingkatan_id" value="" size="2" />
			<?php
				$this->widget('application.components.widgets.FindTextField',
					array('options' => array('id'=>'kelas_tahun_ajaran_name_asal','value'=>'','disabled'=>''),
							'windowParams' => array('url'=>"index.php?r=data_popup/kelasTahunAjaran",
									'specs'=> array('height'=>300,'width'=>800),
									'name'=> "popKelasTahunAjaran",
							),
							'callback'=>"getKelasTahunAjaranAsal",
							'beforePopupFunc'=>"kelasTahunAjaranAsalBeforePopup",
							'formId'=>"frmFilter",
					)
				);
			?>
		</div>
			
		<table id="tblData"><tr></tr></table>
		<div id="navTblData"></div>
	</div>
	
	<div style="float:left;width:100px;margin-left:20px;margin-top:100px">
		<img src="images/action/Actions-go-next-icon.png" style="width:100px;" />
	</div>
	
	
	<div style="float:left;margin-left:20px;width:500px">
		<div style="margin-bottom:10px">
			<label class="form mandatory" style="width:100px"><?php echo Yii::t("label","Kelas tujuan")?></label>
			<input type="hidden" id="kelas_tahun_ajaran_id_tujuan" name="kelas_tahun_ajaran_id_tujuan" value="" size="2" />		
			<?php
				$this->widget('application.components.widgets.FindTextField',
					array('options' => array('id'=>'kelas_tahun_ajaran_name_tujuan','value'=>'','disabled'=>''),
							'windowParams' => array('url'=>"index.php?r=data_popup/kelasTahunAjaran",
									'specs'=> array('height'=>300,'width'=>800),
									'name'=> "popKelasTahunAjaran",
							),
							'callback'=>"getKelasTahunAjaranTujuan",
							'beforePopupFunc'=>"kelasTahunAjaranTujuanBeforePopup",
							'formId'=>"frmFilter",
					)
				);
			?>
		</div>
		<table id="tblData2"><tr></tr></table>
		<div id="navTblData2"></div>
	</div>
	<div style="clear:both"></div>
</div>